import Vue from "vue";
import Vuex from "vuex";
import axios from 'axios'

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    list:[],
    inputvalue:'',
    nextid:4
  },
  getters:{
    dwg(state){
      return state.list.filter((value)=>{return value.done ===false}).length
    }
  },
  mutations: {
    fetchlist(state,data){
      state.list = data
    },
    fetchdata(state,data1){
       state.inputvalue = data1
    },
    handle3(state){
       const obj = {
        id:state.nextid,
        info:state.inputvalue.trim(),
        done:true
       }
       state.list.push(obj);
       state.nextid++
       state.inputvalue='';

    },
    fetchindex(state,id){
      const i = state.list.findIndex((x)=>{return x.id===id} )
       if(i!==-1) {
        state.list.splice(i,1)
       }
    },
    fetchone(state,pam){
    const i = state.list.findIndex((e)=>{return e.id === pam.id});
    if(i!==-1) {
      state.list[i].done = pam.done
    }
    }
  },
  actions: {
    // 在actions中不能直接操作state中的数据
    getlist(context){
       axios.get('/index.json').then((res)=>{
        //  console.log(res.data);
         context.commit('fetchlist',res.data)
       })
    }
  },
  modules: {},
});
